﻿using System.Windows.Input;
using Oxage.CNC.Commands;
using Oxage.CNC.Hardware;
using Oxage.CNC.Views;
using Oxage.Common;

namespace Oxage.CNC.Models
{
	public class MainViewModel : ViewModelBase
	{
		#region Constructor
		public MainViewModel()
		{
			this.Document = new DocumentViewModel();

			//Sample G code document
			this.Document.Content =
@";comment
{another comment}
N001 X0 Y0 Z0 G01  ; absolute coordinates by default
N002 X100 Y100 Z0 G02
N003 X0 Y200 Z0 G02
N004 X100 Y300 Z0 G02
N005 X0 Y400 Z0 G02
N006 X100 Y500 Z0 G02";

			//ApplicationCommands.Save
			this.ApplicationCommand = new ApplicationCommand();
			this.DocumentCommand = new DocumentCommand() { Document = Document };
			this.MachineCommand = new MachineCommand() { ViewModel = this };
			this.SimulatorCommand = new SimulatorCommand() { ViewModel = this };

			//Default controller
			var port = new SerialPortMachineController();
			port.SimulatorBlockReceived += (sender, e) =>
			{
				if (View != null)
				{
					View.Simulator.ViewModel.SimulateBlock(e.Result);
				}
			};
			this.machineController = port;

#if DEBUG
			//For debugging on machine without serial port
			this.machineController = new VirtualMachineController();
#endif
		}
		#endregion

		#region Properties
		public MainView View
		{
			get;
			set;
		}

		public DocumentViewModel Document
		{
			get
			{
				return this.document;
			}
			set
			{
				if (this.document != value)
				{
					this.document = value;
					OnPropertyChanged("Document");
				}
			}
		}
		private DocumentViewModel document;

		/// <summary>
		/// Gets or sets a global instance of IMachineController so it can be accessed from other windows.
		/// </summary>
		public IMachineController MachineController
		{
			get
			{
				return this.machineController;
			}
			set
			{
				if (this.machineController != value)
				{
					this.machineController = value;
					OnPropertyChanged("MachineController");
				}
			}
		}
		private IMachineController machineController;

		public ICommand ApplicationCommand
		{
			get
			{
				return this.applicationCommand;
			}
			set
			{
				if (this.applicationCommand != value)
				{
					this.applicationCommand = value;
					OnPropertyChanged("ApplicationCommand");
				}
			}
		}
		private ICommand applicationCommand;

		public ICommand DocumentCommand
		{
			get
			{
				return this.documentCommand;
			}
			set
			{
				if (this.documentCommand != value)
				{
					this.documentCommand = value;
					OnPropertyChanged("DocumentCommand");
				}
			}
		}
		private ICommand documentCommand;

		public ICommand MachineCommand
		{
			get
			{
				return this.machineCommand;
			}
			set
			{
				if (this.machineCommand != value)
				{
					this.machineCommand = value;
					OnPropertyChanged("MachineCommand");
				}
			}
		}
		private ICommand machineCommand;

		public ICommand SimulatorCommand
		{
			get
			{
				return this.simulatorCommand;
			}
			set
			{
				if (this.simulatorCommand != value)
				{
					this.simulatorCommand = value;
					OnPropertyChanged("SimulatorCommand");
				}
			}
		}
		private ICommand simulatorCommand;
		#endregion
	}
}
